'''
Created on 19 Feb 2012

@author: gav.aiken
'''
from google.appengine.ext import db

def CreateGame(playerId, playerTeam,  playerScore, opponentId, opponentTeam, opponentScore):
    '''
    Factory Method
    '''
    game = Game(playerId=playerId, playerTeam=playerTeam,  playerScore=playerScore, 
                opponentId=opponentId, opponentTeam=opponentTeam, opponentScore=opponentScore)
    
    game.goalDifference = playerScore - opponentScore
   
    if game.goalDifference > 0:
        game.result = 'W'
    elif game.goalDifference == 0:
        game.result = 'D'
    else:
        game.result = 'L'
        
    return game

class Game(db.Model):
    '''
    Represents a single played game
    '''
    playerId        = db.StringProperty(required=True)
    playerTeam      = db.StringProperty(required=True)
    playerScore     = db.IntegerProperty(required=True)
    
    opponentId      = db.StringProperty(required=True)
    opponentTeam    = db.StringProperty(required=True)
    opponentScore   = db.IntegerProperty(required=True)
    
    # Derived
    scrapedDateTime  = db.DateTimeProperty(auto_now_add=True)
    result           = db.StringProperty()
    goalDifference   = db.IntegerProperty()
    
    def __str__(self):
        return '%s : %s (%s) %d - %d %s (%s)' % \
            (self.result, self.playerId, self.playerTeam, self.playerScore, self.opponentScore, self.opponentId, self.opponentTeam)